﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aga.GestionDeArchivos.Domain.Entities;
using Aga.GestionDeArchivos.Storage;
using System.Data.Objects.DataClasses;
using System.Web;
using Aga.GestionDeArchivos.Common;

namespace Aga.GestionDeArchivos.Services
{
    /// <summary>
    /// Base methods for domain services
    /// </summary>
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
    /// <typeparam name="TDto">The type of the dto.</typeparam>
    public abstract class DomainServiceBase<TEntity> : ServiceBase
        where TEntity: class, IDomainEntity
    {

        protected abstract RepositoryBase<TEntity> Repository { get; }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> GetAll()
        {
            var entities = Repository.GetAll();

            return entities;
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public virtual TEntity GetById(int id)
        {
            TEntity entity = Repository.GetById(id);

            return entity;
        }

        /// <summary>
        /// Saves the specified dto.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public virtual TEntity Save(TEntity entity)
        {
            Repository.Save(entity);

            return entity;
        }

        /// <summary>
        /// Deletes the specified dto.
        /// </summary>
        /// <param name="dto">The dto.</param>
        public virtual void Delete(int id)
        {
            TEntity entity = Repository.GetById(id);

            if (entity != null)
            {
                Repository.Delete(entity);
            }

            return;
        }
        
        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <returns></returns>
        public new string GetCurrentUser()
        {
            string user = "anonymous";

            if (HttpContext.Current != null)
                user = HttpContext.Current.User.Identity.Name;

            return user;
        }

    }
}
